//
//  LevelManager.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "LevelManager.h"

@implementation LevelManager
@synthesize _mode = mMode, _speedZoom = mSpeedZoom, _listBulletRate = mListBulletRate;

static LevelManager *m_Instance = nil;

+(LevelManager *) shareLevelManager
{
    if (m_Instance == nil)
        m_Instance = [[LevelManager alloc] init];
    return m_Instance;
}

-(id) init
{
    if (self = [super init])
    {
        mListBulletRate = [[NSMutableArray alloc] init];
        mListLevel =[[NSArray alloc] initWithObjects:
                      @"Level_Template",
                      @"Level_Template_2", nil];
    }
    
    return self;
}

-(void) setCurrentLevelt:(int)currentLevel
{
    mCurrentLevel = currentLevel;
}

-(void) loadCurrentLevel
{
    if (mCurrentLevel > mListLevel.count)
    {
        CCLOG(@"level is out of range");
    }
    
    NSString *path = [mListLevel objectAtIndex:mCurrentLevel];
    [self loadLevel:path];
}

-(void) loadLevel:(NSString *)path
{
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:path ofType:@"xml"];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSError *error;
    
    mDataXMLDocument = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    
    if (mDataXMLDocument == nil)
    {
        // process here
        CCLOG(@"document = nil");
    }
    
    [xmlData release];
    xmlData = nil;
    
    [self loadLevelInfo];
    [self loadComboInfo];
}

-(void) loadComboInfo
{
    NSMutableArray *listCombo = [[[NSMutableArray alloc] init] autorelease];
    NSArray *listLevelInfo = [mDataXMLDocument.rootElement elementsForName:XML_LEVEL];
    for (GDataXMLElement *eLevel in listLevelInfo)
    {
        NSArray *listComboInfo = [eLevel elementsForName:XML_COMBO];
        for (GDataXMLElement *eCombo in listComboInfo)
        {
            int score = [[[eCombo attributeForName:XML_SCORE] stringValue] intValue];
            int item = [[[eCombo attributeForName:XML_ITEM] stringValue] intValue];
            [listCombo addObject:[NSNumber numberWithInt:score]];
            [listCombo addObject:[NSNumber numberWithInt:item]];
        }
    }
    
    [[Combo shareCombo] setListComboInfo:listCombo];
}

-(void) loadLevelInfo
{
    NSArray *listLevelInfo = [mDataXMLDocument.rootElement elementsForName:XML_LEVEL];
    for (GDataXMLElement *eLevel in listLevelInfo)
    {
        mMode = (LEVEL_MODE)([[[eLevel attributeForName:XML_MODE] stringValue] intValue]);
        mSpeedZoom = [[[eLevel attributeForName:XML_SPEED] stringValue] floatValue];
        
        // load bullet rate
        NSArray *listBulletRate = [eLevel elementsForName:XML_RATE_BULLET];
        for (GDataXMLElement *eBulletRate in listBulletRate)
        {
            int rr = [[[eBulletRate attributeForName:XML_RATE_RED] stringValue] intValue];
            int rg = [[[eBulletRate attributeForName:XML_RATE_GREEN] stringValue] intValue];
            int rb = [[[eBulletRate attributeForName:XML_RATE_BLUE] stringValue] intValue];
            int ry = [[[eBulletRate attributeForName:XML_RATE_YELLOW] stringValue] intValue];
            int rp = [[[eBulletRate attributeForName:XML_RATE_PURPLE] stringValue] intValue];
            
            [mListBulletRate addObject:[NSNumber numberWithInt:rr]];
            [mListBulletRate addObject:[NSNumber numberWithInt:rg]];
            [mListBulletRate addObject:[NSNumber numberWithInt:rb]];
            [mListBulletRate addObject:[NSNumber numberWithInt:ry]];
            [mListBulletRate addObject:[NSNumber numberWithInt:rp]];
        }
    }
}

-(CircleOrbitManager *) getCircleOrbitManager
{
    NSArray *listElementCircleOrbit = [mDataXMLDocument.rootElement elementsForName:XML_CIRCLE_ORBIT];
    NSMutableArray *listCircleOrbit = [[NSMutableArray alloc] init];
    for (GDataXMLElement *eCircleOrbit in listElementCircleOrbit)
    {
        NSMutableArray *listCircle = [[NSMutableArray alloc] init];
        NSMutableArray *listCircleSafeZone = [[NSMutableArray alloc] init];
        NSArray *listElementCircle = [eCircleOrbit elementsForName:XML_CIRCLE];
        NSArray *listElementCircleSafeZone = [eCircleOrbit elementsForName:XML_CIRCLE_SAFE_ZONE];
        for (GDataXMLElement *eCircle in listElementCircle)
        {
            NSMutableArray *listCircleSegment = [[NSMutableArray alloc] init];
            NSArray *listElementCircleSegment = [eCircle elementsForName:XML_CIRCLE_SEGMENT];
            for (GDataXMLElement *eCircleSegment in listElementCircleSegment)
            {
                int csType = [[[eCircleSegment attributeForName:XML_TYPE] stringValue] intValue];
                int csColorType = [[[eCircleSegment attributeForName:XML_COLOR_TYPE] stringValue] intValue];
                float csRadius = [[[eCircleSegment attributeForName:XML_RADIUS] stringValue] floatValue];
                
                CircleSegment *circleSegment = [[CircleSegment alloc] initWithType:(SEGMENT_TYPE)(csType) color:(COLOR_TYPE)(csColorType) radius:csRadius];
                [listCircleSegment addObject:circleSegment];
                
                [circleSegment release];
                circleSegment = nil;
            }
            
            float cAngle = [[[eCircle attributeForName:XML_ANGLE] stringValue] floatValue];
            
            Circle *circle = [[Circle alloc] initWithAngle:cAngle listSegment:listCircleSegment];
            [listCircle addObject:circle];
            
            [circle release];
            circle = nil;
            [listCircleSegment release];
            listCircleSegment = nil;
        }
        
        for (GDataXMLElement *eCircleSafeZone in listElementCircleSafeZone)
        {
            float cszStartAngle = [[[eCircleSafeZone attributeForName:XML_START_ANGLE] stringValue] floatValue];
            float cszRange = [[[eCircleSafeZone attributeForName:XML_RANGE] stringValue] floatValue];
            float cszWidth = [[[eCircleSafeZone attributeForName:XML_WIDTH] stringValue] floatValue];
            
            CircleSafeZone *circleSafeZone = [[CircleSafeZone alloc] initWithStartAngle:cszStartAngle range:cszRange width:cszWidth];
            [listCircleSafeZone addObject:circleSafeZone];
            [circleSafeZone release];
        }
        
        float coRadius = [[[eCircleOrbit attributeForName:XML_RADIUS] stringValue] floatValue];
        float coStartAngle = [[[eCircleOrbit attributeForName:XML_START_ANGLE] stringValue] floatValue];
        float coSpeed = [[[eCircleOrbit attributeForName:XML_SPEED] stringValue] floatValue];
        
        CircleOrbit *circleOrbit = [[CircleOrbit alloc] initWithListCircle:listCircle];
//        [circleOrbit addListCircleSafeZone:listCircleSafeZone];
        [circleOrbit setRadius:coRadius];
        [circleOrbit setStartAngle:coStartAngle];
        [circleOrbit setSpeed:coSpeed];
        [listCircleOrbit addObject:circleOrbit];
        
        [circleOrbit release];
        circleOrbit = nil;
        [listCircle release];
        listCircle = nil;
        [listCircleSafeZone release];
        listCircleSafeZone = nil;
    }

    CircleOrbitManager *circleOrbitManager = [[[CircleOrbitManager alloc] initWithListCircleOrbit:listCircleOrbit] autorelease];

    [listCircleOrbit release];
    listCircleOrbit = nil;
    
    return circleOrbitManager;
}

-(NSArray *) getListOrbitRadius
{
    NSMutableArray *listOrbitRadius = [[[NSMutableArray alloc] init] autorelease];
    NSArray *listElementOrbitIndicator = [mDataXMLDocument.rootElement elementsForName:XML_ORBIT_INDICATOR];
    for (GDataXMLElement *eOrbitRadius in listElementOrbitIndicator)
    {
        float radius = [[[eOrbitRadius attributeForName:XML_RADIUS] stringValue] floatValue];
        [listOrbitRadius addObject:[NSNumber numberWithFloat:radius]];
        
        if ([LevelManager shareLevelManager]._mode == MODE_SAVE_PLANET)
        {
            float range = [[[eOrbitRadius attributeForName:XML_RANGE] stringValue] floatValue];
            float dzradius = [[[eOrbitRadius attributeForName:XML_DANGER_ZONE_RADIUS] stringValue] floatValue];
            [listOrbitRadius addObject:[NSNumber numberWithFloat:range]];
            [listOrbitRadius addObject:[NSNumber numberWithFloat:dzradius]];
        }
    }
    
    return listOrbitRadius;
}

-(void) removeData
{
    [mDataXMLDocument release];
    mDataXMLDocument = nil;
}

@end


